// pages/article-detail/detail.js
var model = require('../composition/model/article.js')
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      baseInfo:{},
      content:"",
      comment:{},
      ak:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var _baseInfo=options.baseInfo;
    console.log("baseInfo:%o:",_baseInfo)
    if (_baseInfo == null || typeof (_baseInfo) =='undefined'){
      wx.navigateBack({});
    }
    _baseInfo = JSON.parse(_baseInfo)
    this.setData({
      baseInfo: _baseInfo
    })
    var that=this
    model.loadDetail(_baseInfo.id,function(res){
        if(res){
          that.setData({
            content: res.content,
            ak:res.ak,
            comment: res.comment
          })
          WxParse.wxParse('article', 'html', res.content, that, 5);
          that.report(that.data.baseInfo.id, res.ak, "click")
        }
    })
  },
  //分享
  shareArticle:function(event){
    wx.showToast({
      title: '请稍等...',
      icon: ''
    })
  },
  //点赞
  supportArticle:function(event){
    wx.showLoading({
      title: '马上就好...',
    })
    this.report(this.data.baseInfo.id, this.data.ak,"zan")
  },
  report:function(_no,_ak,_action){
    model.rePort(_no,_ak,_action,function(res){
      if (_action=='zan'){
        wx.hideLoading()
        wx.showToast({
          title: '已经收到您的赞，感谢您的支持',
          icon: 'none'
        })
      }  
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var obj={
      author:this.data.baseInfo.author,
      boutique: this.data.baseInfo.boutique,
      clickNum: this.data.baseInfo.clickNum,
      id: this.data.baseInfo.id,
      score: this.data.baseInfo.score,
      title: this.data.baseInfo.title,
      zanNum: this.data.baseInfo.zanNum
    }
    var shareObject = {
      title: "发现一篇美文【"+this.data.baseInfo.title+"】",
      path: 'pages/article-detail/detail?baseInfo=' + JSON.stringify(obj),
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          icon: ''
        })
      }
    }
    return shareObject;
  }
})